package com.borqs.samples;

import android.util.*;
import android.os.*;
import android.content.*;
import com.android.internal.app.IBatteryStats;
import com.android.internal.os.BatteryStatsImpl;
import android.content.pm.PackageManager;
import android.os.BatteryStats.Uid;

public class BatteryStatsSampler extends ASampler{
    private final static String TAG = "BatteryStatsSampler";
    private final int which = BatteryStats.STATS_SINCE_UNPLUGGED;
    private IBatteryStats mBatteryStats;
    private BatteryStatsImpl mStats;

    public BatteryStatsSampler(DBHelper helper){
        super(helper);
        mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService("batteryinfo"));
    }
    @Override public void sample(long sampletime){
    }
    /*battery stats things*/
    private void testBatteryStats(){
        long load_begin = System.currentTimeMillis();
        load();//reload mStats object
        //networkSummay();
        long load_end = System.currentTimeMillis();

        Log.d(TAG,"isOnBattery="+mStats.isOnBattery());

        /*time things i care*/
        long now = System.currentTimeMillis();
        long upnow = SystemClock.uptimeMillis()*1000;
        long realnow = SystemClock.elapsedRealtime()*1000;
        Log.d(TAG,"now="+now+",upnow="+Util.getTimeDuration(upnow/1000)+",realnow="+Util.getTimeDuration(realnow/1000));
        long battery_uptime = mStats.computeBatteryUptime(upnow,which);
        long battery_realtime = mStats.computeBatteryRealtime(realnow,which);
        Log.d(TAG,"battery_uptime="+Util.getTimeDuration(battery_uptime/1000));
        Log.d(TAG,"battery_realtime="+Util.getTimeDuration(battery_realtime/1000));

        long total_uptime = mStats.computeUptime(upnow,which);
        long total_realtime = mStats.computeRealtime(realnow,which);
        Log.d(TAG,"total_uptime="+Util.getTimeDuration(total_uptime/1000));
        Log.d(TAG,"total_realtime="+Util.getTimeDuration(total_realtime/1000));

        /*tcp data*/
        long mobile_tcp_tx = mStats.getMobileTcpBytesSent(which);
        long mobile_tcp_rx = mStats.getMobileTcpBytesReceived(which);
        Log.d(TAG,"mobile_tcp_tx="+mobile_tcp_tx);
        Log.d(TAG,"mobile_tcp_rx="+mobile_tcp_rx);

        long total_tcp_tx = mStats.getTotalTcpBytesSent(which);
        long total_tcp_rx = mStats.getTotalTcpBytesReceived(which);
        Log.d(TAG,"total_tcp_tx="+total_tcp_tx);
        Log.d(TAG,"total_tcp_rx="+total_tcp_rx);

        /*uid 's things*/
        //PackageManager pm = this.getPackageManager();

        SparseArray<? extends Uid> uidStats = mStats.getUidStats();

        long uid_begin = System.currentTimeMillis();
        final int NU = uidStats.size();
        for (int iu = 0; iu < NU; iu++) {
            Uid u = uidStats.valueAt(iu);
            int myuid = u.getUid();

            //Log.d(TAG,"uid="+myuid+"---"+pm.getNameForUid(myuid));
            /*
            if(myuid == 10067){
                //this is 'com.sina.weibo'
            }
            */
            long tcp_received = u.getTcpBytesReceived(which);
            long tcp_sended = u.getTcpBytesSent(which);
            //Log.d(TAG,"uid="+myuid+",tcp_received="+tcp_received+",tcp_sended="+tcp_sended);
            //Log.d(TAG,"currentTcpReceived="+((BatteryStatsImpl.Uid)u).computeCurrentTcpBytesReceived());
            //Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
        }

        long uid_end = System.currentTimeMillis();
        Log.d(TAG,"load time="+(load_end-load_begin));
    }
    private void load() {
        try {
            byte[] data = mBatteryStats.getStatistics();
            Log.d(TAG,"length="+data.length);
            Parcel parcel = Parcel.obtain();
            parcel.unmarshall(data, 0, data.length);
            parcel.setDataPosition(0);
            mStats = com.android.internal.os.BatteryStatsImpl.CREATOR
                    .createFromParcel(parcel);
            mStats.distributeWorkLocked(BatteryStats.STATS_SINCE_UNPLUGGED);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException:", e);
        }
    }
}
